<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="ck" uri="http://ckeditor.com"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Student Exam</title>
<% Integer time = (Integer)request.getSession().getAttribute("examTime"); %>
<% Integer examSubjectId = (Integer)request.getSession().getAttribute("examSubjectId"); %>
<script src="${pageContext.request.contextPath}/static/ckeditor/ckeditor.js"></script>
<script src="${pageContext.request.contextPath}/static/ckeditor/config.js"></script>
<script>
	function Print() {
	    var tit = document.title;
	    document.title = "";
	    table1.style.display = "none";
	    window.print();
	    document.title = tit;
	    table1.style.display = ""; 
	}
</script>
 <script type="text/javascript">
   	/* $(function(){
   	 CKEDITOR.repalce('tiName');
   	});  */         		
</script>
<script type="text/javascript">
	$(function(){
		$("input:radio[name='examTiOption']").click(function(){
			var inputData = $("input:radio[name='examTiOption']:checked").val();
		})
	})
</script>

<!-- <script>

function onSave(id,subjectId){
	   var array=new Array()
	   for(var i=1;i<=id;i++){
	       var inputData=$("input:radio[name='examTiOption"+i+"']:checked").val();
	      //array//再想想
	       array[array.length]=inputData;
	      
 	   }
	   //数组转换成json格式
	   var jsonString = JSON.stringify(array);
	    $.ajax({   
				    	  //判断提交数据类型
				    	 type:"post",
				    	 //url地址
				    	 url:"${pageContext.request.contextPath}/exam/assignment"+subjectId+"",
				    	 //数据类型
				    	
	    	            data: {
	    	            	array: jsonString
	    	            },
				    	 async : true,
				    	 //receive the parameter after successing
				    	 success: function(data){
				    		
				    		if("success"==data.result){
				    		  
				    			window.location.reload();
				    			
				    		} 
				    	   
				    	   }
				    })	
   }
</script> -->
<script>
    //设置富文本编辑器内容只读
    var editor;
    CKEDITOR.on( 'instanceReady', function( ev ) {
       editor = ev.editor;
       editor.setReadOnly( true );
    });
</script>
<SCRIPT>
var totalTime=parseInt("${totaltime}")/1000;
var parseTime=parseInt("${sparetime}")/1000;
var cd;
var cache;
$(function(){
	cache=setInterval(function(){
		saveToService();
		}, 10000);
	countdown();
});
function saveToService(){
	$.ajax({
		dataType:"json",
		type:"post",
		url:"${pageContext.request.contextPath }/exam/time?ajax=true",
		cache:false,
		success:function(data){
			if(data.result=='success'){
				//comTimeout(data.spareTime);
				spareTime=data.spareTime/1000;
				countdown();
				}
			},
		error:function(){
			
			},
	});
}

function countdown(){
	if(cd!=null)
		clearInterval(cd);
	cd=setInterval(function() {
	if(parseTime>0){
		$("#msg").html('剩余时间:'+timeChange(parseTime));
		parseTime--;
	}
}, 1000)
}
function timeChange(time){
	var hour=Math.floor(time/3600);
	var mintute=Math.floor((time%3600)/60);
	var second=Math.floor((time%3600)%60);
	var str='';
	if(hour!=0)
		str += hour+'小时 ';
	if(mintute!=0)
		str += mintute+'分钟 ';
	if(second!=0)
		str += second+'秒';
	if(hour<=0 && mintute<=0 && second<=0)
		str +="考试结束";
	return str;
}
</SCRIPT>
</head>
<body>

	<%-- <form action="${pageContext.request.contextPath}/examstart/count" method="post" id="form">
<c:forEach items="${stuTiList}" var="stuTiList" begin="1" varStatus="i" > 
<c:if test="${stuTiList.examTiType==0}">
	<p>题目：${stuTiList.examTiName}</p>
	<p>A:<input type="radio" name="examti${i.index}" value="A">${stuTiList.examTiOptiona}</p>
	<p>B:<input type="radio" name="examti${i.index}" value="B">${stuTiList.examTiOptionb}</p>
	<p>C:<input type="radio" name="examti${i.index}" value="C">${stuTiList.examTiOptionc}</p>
	<p>D:<input type="radio" name="examti${i.index}" value="D">${stuTiList.examTiOptiond}</p>
</c:if>
<c:if test="${stuTiList.examTiType==1}">
	<p>题目：${stuTiList.examTiName}</p>
	<p>A:<input type="checkbox" name="examti${i.index}" value="A">${stuTiList.examTiOptiona}</p>
	<p>B:<input type="checkbox" name="examti${i.index}" value="B">${stuTiList.examTiOptionb}</p>
	<p>C:<input type="checkbox" name="examti${i.index}" value="C">${stuTiList.examTiOptionc}</p>
	<p>D:<input type="checkbox" name="examti${i.index}" value="D">${stuTiList.examTiOptiond}</p>
</c:if>
</c:forEach>
<input type="submit" value="提交试卷">
</form> --%>
	
		<strong style="color:blue"><span id="msg"></span></strong>
	 <form:form action="${pageContext.request.contextPath}/student/assignment">
		<c:set var="i" value="${0}"></c:set>
        <%int i=0;%>
           <c:forEach items="${stuTiList}" var="stuTiList">
           <%i++;%>
           <div class="panel panel-default">
               <p class="panel-heading">第${stuTiList.examTiId}题</p>
               <div class="panel-body">
                      	<textarea name="tiName">${stuTiList.examTiName}</textarea>
                      <ck:replace replace="tiName" basePath="ckeditor"></ck:replace>
                </div> 
                <div class="panel-body" style="margin-top:0px">
                    <input type="radio" id="examTiOptiona"   name="examTiOption<%=i%>"  value="a"/> ${stuTiList.examTiOptiona}<br>
                    <input type="radio" id="examTiOptionb"   name="examTiOption<%=i%>"  value="b"/> ${stuTiList.examTiOptionb}<br>
                     <input type="radio" id="examTiOptionc"  name="examTiOption<%=i%>"  value="c"/> ${stuTiList.examTiOptionc}<br>
                     <input type="radio" id="examTiOptiond"  name="examTiOption<%=i%>"  value="d"/> ${stuTiList.examTiOptiond}<br>
                </div>
            </div>
      </c:forEach>
      <div>
			<input class="btn btn-info btn-xs" style="margin-top:0px;margin-left:74%;margin-right:2%" type="button" onclick="onSave('<%=i%>','<%=examSubjectId %>')" value="交卷"/>
		 <input id="table1" type="button" value="打印" class="btn btn-info btn-xs" onclick="Print()">
		 
		</div>
</form:form>
   <script>
	function onSave(id,idid){
	   var array=new Array()
	   for(var i=1;i<=id;i++){
	       var inputData=$("input:radio[name='examTiOption"+i+"']:checked").val();
	      //array//再想想
	       array[array.length]=inputData;
	      
 	   }
	   //数组转换成json格式
	   var jsonString = JSON.stringify(array);
	    $.ajax({   
				    	  //判断提交数据类型
				    	 type:"GET",
				    	 //url地址
				    	 url:"${pageContext.request.contextPath}/exam/choose/"+idid,
				    	 //数据类型
				    	
	    	            data: {
	    	            	array: jsonString
	    	            },
				    	 async : true,
				    	 //receive the parameter after successing
				    	 success: function(data){
				    		
				    		  	console.log(data);
				    		if(data.result="success66"){
				    			//window.location.reload();
				    			console.log(666);
				    			window.location.href="${pageContext.request.contextPath}/exam/stuGrade";
				    			
				    		} 
				    	   
				    	   }
				    })	
   }
</script>
</body>
</html>